package android.contest.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.contest.alarmclock.Alarms;
import android.contest.util.Log;
import android.contest.weather.Weather;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

public class ConnectChangeReceiver extends BroadcastReceiver{
	public ConnectChangeReceiver(){
		super();
	}
	@Override
	public void onReceive(Context context, Intent intent) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );   
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();   
        NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE );
        boolean rv = false;
        if ( activeNetInfo != null ) {   
            Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
			Log.v(Log.LOGCONN, "Active Network Type : " + activeNetInfo.getTypeName());
        }   
        if( mobNetInfo != null ) {   
            Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();   
			Log.v(Log.LOGCONN, "Mobile Network Type : " + mobNetInfo.getTypeName());
        }   
        
		if (activeNetInfo == null){
			Log.v(Log.LOGCONN, "isConnected(): info == null");
            Toast.makeText( context, "isConnected(): info == null", Toast.LENGTH_SHORT ).show();
		}else{
	        if(activeNetInfo.isConnected()){
	        	Log.v(Log.LOGCONN, "isConnected(): state: connected");
				Toast.makeText(context, "isConnected(): tomorrow condition is "+Weather.TmrwCond(),Toast.LENGTH_SHORT).show();;
	//			Toast.makeText(context, "isConnected(): info.isConnected() == true",Toast.LENGTH_SHORT).show();;
//	        	if(Weather.SatisfyCondition(context)){
//	    			Toast.makeText(context, "isConnected(): condition satisfied",Toast.LENGTH_SHORT).show();;
//	//        		Alarms.setWashingNotify(context);
//	        	}
				rv = Weather.saveWeatherFile(context);//once the Internet is available, save the weather to the local xml file
	        }
	        else{
	        	Log.v(Log.LOGCONN, "isConnected(): state: disconnected!!");
				Toast.makeText(context, "isConnected(): disconnected!!",Toast.LENGTH_SHORT).show();;
	        }
		}
		Log.v(Log.LOGWEATHER, "ConnectChangeReceiver.onReceiver(): saving file " + (rv?"Success":"Failure"));
	}
}